package com.pets.controller;

import com.pets.entity.Apply;
import com.pets.qo.ApplyQO;
import com.pets.service.ApplyService;
import com.pets.util.Result;
import com.pets.vo.UserApplyVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 *申请成为机构操作相关控制器
 */
@Controller
@RequestMapping(value = "/apply")
public class ApplyController {

    @Resource
    private ApplyService applyService;


    @RequestMapping(value = "/toApply")
    public ModelAndView toApply(int uId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/user/apply");
        //查询用户的申请记录
        List<UserApplyVO> userApplyVOS = applyService.getUserApplyList(uId);
        modelAndView.addObject("voList",userApplyVOS);
        //判断用户是否可以申请
        boolean flag =true;
        //判断是否有申请记录，没有申请记录可以申请
        if(userApplyVOS.size() == 0){
            flag = true;
        }else {
            //判断申请记录中是否有申请通过的记录、存在申请中的也不予许再申请  0：申请中   1：申请通过
            for (int i = 0; i <userApplyVOS.size() ; i++) {
                if("1".equals(userApplyVOS.get(i).getAppstatus()) || "0".equals(userApplyVOS.get(i).getAppstatus())){
                    flag = false;
                }
            }
        }
        modelAndView.addObject("flag",flag);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/doApply" ,method = RequestMethod.POST)
    public Result doApply(ApplyQO applyQO){
        return applyService.doApply(applyQO);
    }

    @ResponseBody
    @RequestMapping(value = "/cancelApply")
    public Result cancelApply(int appId){
        return applyService.cancelApply(appId);
    }
}
